--pregunta 1

select * from Production.ProductListPriceHistory
select * from Production.Product

declare @idpro bigint
		,@name nvarchar(100)
		,@price money
declare @temp table(nombre varchar(100),PrecioAnterior money,MontoIncremento money, NuevoPrecio money)
declare qUpdatePrice cursor for
	select pro.productId,pro.name,pro.listPrice
		from production.Product	pro
open qUpdatePrice
FETCH NEXT FROM qUpdatePrice INTO @idpro,@name,@price
While @@FETCH_STATUS = 0
begin
	--try
	--begin
	--actualizado de precios
	if( @price <100)
		begin
			update Production.Product 
			set ListPrice=@price + (@price  * 0.15)
			output @name,deleted.ListPrice,(@price  * 0.15),inserted.ListPrice into @temp
			where productId=@idpro
			--registro de modificacion
				insert into Production.ProductListPriceHistory
				values(@idpro,CURRENT_TIMESTAMP,DATEADD(year,1,CURRENT_TIMESTAMP),@price + (@price  * 0.15),CURRENT_TIMESTAMP)
		end
	else
		begin
			update Production.Product 
			set ListPrice=@price  + (@price  * 0.1)
			output @name,deleted.ListPrice,(@price  * 0.1),inserted.ListPrice into @temp
			where productId=@idpro
			--registro de modificacion
				insert into Production.ProductListPriceHistory
				values(@idpro,CURRENT_TIMESTAMP,DATEADD(year,1,CURRENT_TIMESTAMP),@price + (@price  * 0.1),CURRENT_TIMESTAMP)
		end
	--end
	--catch
	--begin
	--	print 'error'
	--end
	FETCH NEXT FROM qUpdatePrice INTO @idpro,@name,@price
END
CLOSE qUpdatePrice
DEALLOCATE qUpdatePrice

select CURRENT_TIMESTAMP


-- pregunta 2
create table registroActividad 
	(ID int identity(1,1)
	,DatosAnteriores nvarchar(max)
	,NuevosDatos nvarchar(max)
	,OperacionRealizada varchar(15)
	,FechaActualizacion date default(current_timeStamp))
declare @pri int=0
		,@seg int=1
		,@ter int=0

declare @fibonachi table (nro int)
--create table fibonachi(nro int)
insert into @fibonachi values(@pri)
insert into @fibonachi values(@seg)


while @pri+@seg<30000
begin
	set @ter=@pri+@seg
	insert into @fibonachi values(@ter)
	set @pri=@seg
	set @seg=@ter
end

--select BusinessEntityID,FirstName,LastName
--			from AdventureWorks2012.person.person pe 
--				join @fibonachi fib on fib.nro=pe.BUsinessEntityID
--select * from @fibonachi
--select * from Employees
--select * from AdventureWorks2012.person.person

--merge into (select EmployeeID,FirstName,LastName from Employees) as tgt
merge into Employees as tgt
using (select distinct BusinessEntityID,FirstName,LastName
			from AdventureWorks2012.person.person pe 
				join @fibonachi fib on fib.nro=pe.BUsinessEntityID) as src on tgt.EmployeeId=src.BusinessEntityID
when matched then update 
	set tgt.FirstName=src.FirstName
	,tgt.LastName=src.LastName
when not matched then insert (FirstName,LastName)
	--set identity_insert off
	values(src.FirstName,src.LastName) 
	output isnull(concat(deleted.FirstName,' ',deleted.LastName),' '),concat(inserted.FirstName,' ',inserted.LastName),$action into registroActividad;

	select EmployeeID,FirstName,LastName from Employees